/**************************************************************************//**
 * @item     CosyOS Kernel
 * @file     sv_tick.c
 * @brief    滴答服务（仅在滴答钩子、定时中断钩子、定时查询钩子中调用）
 * @author   迟凯峰
 * @version  V3.3.1
 * @date     2023.07.26
 ******************************************************************************/

#include "os_var.h"
#include "os_api.h"
#include "os_iss.h"



/*
 * 任务
 */

/* 恢复任务 */
tECODE resume_task__(tspTaskNode task_node) __C51USING__
{
	#if SYSCFG_TASKCREATEMODE == __DYNAMIC__
	if(task_node == NULL) return ECODE_TASKNOTSTARTED;
	#endif
	if(task_node->state & __SUSPENDED__)
	{
		task_node->state &= __RESUME__;
		return ECODE_NOERR;
	}
	if(task_node->state < __STOPPED__)  return ECODE_TASKUNSUSPENDED;
	if(task_node->state & __STOPPED__)  return ECODE_TASKSTOPPED;
	if(task_node->state == __DELETED__) return ECODE_TASKNOTSTARTED;
	return ECODE_DONOTKNOW;
}

/* 挂起任务 */
tECODE suspend_task__(tspTaskNode task_node) __C51USING__
{
	#if SYSCFG_TASKCREATEMODE == __DYNAMIC__
	if(task_node == NULL) return ECODE_TASKNOTSTARTED;
	#endif
	if(task_node->state < __SUSPENDED__)
	{
		task_node->state |= __SUSPENDED__;
		return ECODE_NOERR;
	}
	if(task_node->state & __SUSPENDED__) return ECODE_TASKSUSPENDED;
	if(task_node->state & __STOPPED__)   return ECODE_TASKSTOPPED;
	if(task_node->state == __DELETED__)  return ECODE_TASKNOTSTARTED;
	return ECODE_DONOTKNOW;
}

/* 删除任务 */
tECODE delete_task__(tspTaskNode task_node) __C51USING__
{
	#if SYSCFG_TASKCREATEMODE == __DYNAMIC__
	if(task_node == NULL) return ECODE_TASKNOTSTARTED;
	#endif
	if(task_node->state < __DELETED__)
	{
		task_node->state = __DELETED__;
		return ECODE_NOERR;
	}
	if(task_node->state == __DELETED__) return ECODE_TASKNOTSTARTED;
	return ECODE_DONOTKNOW;
}

/* 设置任务优先级 */
tECODE set_priority__(tspTaskNode task_node, u8 tpl) __C51USING__
{
	#if SYSCFG_TASKCREATEMODE == __DYNAMIC__
	if(task_node == NULL) return ECODE_TASKNOTSTARTED;
	#endif
	if(task_node->state < __STOPPED__)
	{
		if(task_node->TPL != tpl)
		{
			task_node->TPL = tpl;
			sSetPriority(task_node);
			return ECODE_NOERR;
		}
		return ECODE_TPLUNCHANGED;
	}
	if(task_node->state & __STOPPED__)  return ECODE_TASKSTOPPED;
	if(task_node->state == __DELETED__) return ECODE_TASKNOTSTARTED;
	return ECODE_DONOTKNOW;
}



/*
 * 延时
 */

/* 设置延时 */
bool set_delay__(tspTaskNode task_node, tDelay tc) __C51USING__
{
	if(task_node->state == __BLOCKED__ && task_node->blocktype == __DELAY__)
	{
		vDELAY_STMR[task_node->TID] = tc;
		return true;
	}
	else
	{
		return false;
	}
}

/* 清除延时 */
bool clr_delay__(tspTaskNode task_node) __C51USING__
{
	if(task_node->state == __BLOCKED__ && task_node->blocktype == __DELAY__)
	{
		vDELAY_STMR[task_node->TID] = 0;
		task_node->state = __READY__;
		return true;
	}
	else
	{
		return false;
	}
}



/*
 * 计数信号量
 */
#if SYSCFG_SEMAPHORE == __ENABLED__
/* 获取 */
bool take_sem__(tSemSize _XDATA_MEM_ *c) __C51USING__
{
	if(*c)
	{
		(*c)--;
		return true;
	}
	return false;
}

/* 释放 */
void free_sem__(tspSem p) __C51USING__
{
	if(p->counter < p->max)
	{
		p->counter++;
	}
}

#endif



/*
 * 飞信
 */
#if SYSCFG_FETION == __ENABLED__
/* 接收飞信 */
tFetion recv_fetion__(tFetion _XDATA_MEM_ *tbox) __C51USING__
{
	tFetion tion = *tbox;
	*tbox = false;
	return tion;
}

#endif



/*
 * 消息邮箱
 */
#if SYSCFG_MAILBOX == __ENABLED__
/* 发送邮件 */
void send_mail__(void _XDATA_MEM_ *gm, void *lm, size_t size, bool _XDATA_MEM_ *gf) __C51USING__
{
	#if SYSCFG_INTRECVMAIL == 0
	sWRITE_MBX_WRITE(gm);
	#elif SYSCFG_INTRECVMAIL == 1
	static tsMbxNode mbx_node;
	mbx_node.gmbx = gm;
	mbx_node.lmbx = lm;
	mbx_node.lnmf = true;
	sWRITE_MBX_WRITE(&mbx_node);
	#endif
	__memcpy(gm, lm, size);
	sWRITE_MBX_WRITE(NULL);
	#if SYSCFG_INTRECVMAIL == 0
	*gf = true;
	#elif SYSCFG_INTRECVMAIL == 1
	*gf = mbx_node.lnmf;
	#endif
}

#endif



/*
 * 消息队列
 */
#if SYSCFG_MSGQUEUE == __ENABLED__
/* 发送消息 */
tECODE send_msg__(tspMsgQueue msg_queue, void *msg) __C51USING__
{
	msg_queue->mutex = false;
	if(msg_queue->counter == msg_queue->len)
	{
		msg_queue->mutex = true;
		#if SYSCFG_DEBUGGING == __ENABLED__
		vAlarm.overflow_msgqueue = true;
		#endif
		return ECODE_OVERFLOW_MSGQUEUE;
	}
	if(msg_queue->type == __DYNAMIC__)
	{
		tspMsgNode msg_node = NULL;
		msg_node = (tspMsgNode)__malloc(sizeof(tsMsgNode));
		if(msg_node == NULL)
		{
			msg_queue->mutex = true;
			#if SYSCFG_DEBUGGING == __ENABLED__
			vFault.mallocfail_msgnode = true;
			#endif
			return ECODE_MALLOCFAIL_MSGNODE;
		}
		sSendMsg_Dynamic(msg);
	}
	else
	{
		sSendMsg_Static(msg);
	}
	msg_queue->mutex = true;
	return ECODE_NOERR;
}

/* 接收消息 */
void *recv_msg__(tspMsgQueue msg_queue) __C51USING__
{
	void *msg = NULL;
	tspMsgNode msg_node;
	msg_queue->mutex = false;
	if(!msg_queue->counter);
	else if(msg_queue->type == __DYNAMIC__)
	{
		sRecvMsg_Dynamic(msg);
	}
	else
	{
		sRecvMsg_Static(msg);
	}
	msg_queue->mutex = true;
	return msg;
}

#endif



/*
 * 全局变量
 */

/* 全局变量写访问 */
void write_gvar__(void *gp, void *lp, size_t size) __C51USING__
{
	static tsVarNode var_node;
	var_node.gvar = gp;
	var_node.lvar = lp;
	sWRITE_VAR_WRITE(&var_node);
	size ? __memcpy(gp, lp, size) : __strcpy((char *)gp, (char *)lp);
	sWRITE_VAR_WRITE(NULL);
}

/* 全局变量自运算 */
void sope_gvar__(void *gp, void *lp, tfpVV fp) __C51USING__
{
	static tsVarNode var_node;
	var_node.gvar = gp;
	var_node.lvar = lp;
	sWRITE_VAR_WRITE(&var_node);
	(*fp)();
	sWRITE_VAR_WRITE(NULL);
}
